package com.xdtech.base.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.base.model.District;
import com.xdtech.base.service.DistrictService;
import com.xdtech.base.vo.DistrictItem;
import com.xdtech.sys.aspect.SystemControllerLog;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-02 14:02:25
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class DistrictController {
	private Log log = LogFactory.getLog(DistrictController.class);
	@Autowired
	private DistrictService districtService;
	@RequestMapping(value="/district.do",params = "district")
	public ModelAndView skipDistrict() {
		return new ModelAndView("base/district/district_ftl");
	}
	
	@RequestMapping(value="/district.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(DistrictItem item,Pagination pg) {
		Map<String, Object> results =  districtService.loadPageDataByConditions(pg,item,"findDistrictByCondition");
		return results;
	}
	
	@RequestMapping(value="/district.do",params = "editDistrict")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editDistrict(HttpServletRequest request,Long districtId) {
		if (districtId!=null) {
			request.setAttribute("districtItem", districtService.loadDistrictItem(districtId));
		}
		return new ModelAndView("base/district/editDistrict_ftl");
	}
	
	@RequestMapping(value="/district.do",params = "saveDistrict")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveDistrict(DistrictItem item) {
		ResultMessage r = new ResultMessage();
		if (districtService.saveOrUpdateDistrict(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/district.do",params = "deleteDistrictItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteDistrictItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> districtIds = new ArrayList<Long>();
			for (String id : tempIds) {
				districtIds.add(Long.valueOf(id));
			}
			districtService.deleteDistrictInfo(districtIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

	@RequestMapping(params = "loadComboBox")
	@ResponseBody
	public Object loadComboBox(String value,String cityId) {
		List<ComboBox> comboBoxsTemp = new ArrayList<ComboBox>();
		List<District> districts = districtService.loadDistrictByCityId(cityId);
		for (District district : districts) {
			comboBoxsTemp.add(new ComboBox(district.getId()+"", district.getName(), district.getId().toString().equals(value)));
		}
		return comboBoxsTemp;
	}
}
